package sq.service.impl;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sq.dao.VerificationInfo;
import sq.repository.VerificationRepository;
import sq.service.VCodeValidationService;

import java.util.Date;

/**
 * 短信验证码验证接口实现类
 *
 * @author sunquan
 */
@Service
public class VCodeValidationServiceImpl implements VCodeValidationService {
    @Autowired
    private VerificationRepository verificationRepository;

    public Integer vCodeValidated(String vCode, String phone) {
        VerificationInfo verificationInfo = verificationRepository.findVerificationInfoByPhone(phone);
        if (verificationInfo == null) {
            return 2;//短信验证码不存在
        }
        if (new Date().getTime() - verificationInfo.getDate().getTime() >= 20) {
            if (verificationInfo.getVerificationCode().equals(vCode)) {
                return -1;//短信发送成功
            } else {
                return 0;//短信验证码输入不匹配
            }
        } else {
            return 1;//短信验证码过期
        }
    }
}
